package com.bsbm.common.menu;
import javax.el.MethodExpression;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;

import org.richfaces.component.Mode;
import org.richfaces.component.UIDropDownMenu;
import org.richfaces.component.UIMenuItem;
import org.richfaces.component.UIToolbar;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import java.io.Serializable;

public class DropDownMenuBean implements Serializable{
    private String current;
    private UIToolbar menuBar;

    public String getCurrent() {
        return this.current;
    }

    public void setCurrent(String current) {
        this.current = current;
    }

    public String getCurrentUserName(){
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getUsername()+" "+user.getAuthorities();
    }

    public String doCreate() {
        this.current = "Member Registration";
        System.out.println("DropDownMenuBean.doCreate()");
        return "/MemberRegistration.xhtml";
    }

    public String doSearch() {
        this.current = "Member Search";
        System.out.println("DropDownMenuBean.doSearch()");
        return "/MemberSearch.xhtml";
    }


    public void setMenuBar(UIToolbar menuBar) {
        this.menuBar = menuBar;
    }

    public UIToolbar getMenuBar() {
        FacesContext ctx = FacesContext.getCurrentInstance();
        menuBar = (UIToolbar) ctx.getApplication()
                .createComponent(ctx, UIToolbar.COMPONENT_TYPE,
                        "org.richfaces.ToolbarRenderer");
        UIDropDownMenu dropDownMenu = (UIDropDownMenu) ctx.getApplication()
                .createComponent(ctx, UIDropDownMenu.COMPONENT_TYPE,
                        "org.richfaces.DropDownMenuRenderer");
        HtmlOutputText label = (HtmlOutputText) ctx.getApplication()
                .createComponent(HtmlOutputText.COMPONENT_TYPE);
        label.setValue("Member");
        dropDownMenu.getFacets().put(UIDropDownMenu.Facets.label.name(), label);
        dropDownMenu.setMode(Mode.ajax);
        dropDownMenu.setHideDelay(0);

        UIMenuItem menItm = (UIMenuItem) ctx.getApplication().createComponent(ctx,
                UIMenuItem.COMPONENT_TYPE,
                "org.richfaces.MenuItemRenderer");
        menItm.setLabel("Create");
       // menItm.setIcon("/images/icons/create_doc.gif");
        Class[] params = {};
        MethodExpression me = ctx.getApplication()
                .getExpressionFactory()
                .createMethodExpression(ctx.getELContext(),
                        "#{dropDownMenuBean.doCreate}", String.class, params);
        menItm.setActionExpression(me);
        menItm.setMode(Mode.ajax);

        //Create an ID on your Item menu:
        menItm.setId("doCreateId");

        dropDownMenu.getChildren().add(menItm);
        // menItm.setOnclick("alert('hello');");
      
            /*UIMenuItem menItm1 = (UIMenuItem) ctx.getApplication().createComponent( 
                    UIMenuItem.COMPONENT_TYPE); 
            menItm1.setMode(Mode.ajax); 
            menItm1.setRendered(false); 
            dropDownMenu.getChildren().add(menItm1);*/
        UIMenuItem menItm2 = (UIMenuItem) ctx.getApplication().createComponent(ctx,
                UIMenuItem.COMPONENT_TYPE,
                "org.richfaces.MenuItemRenderer");
        menItm2.setLabel("Search");
        // menItm2.setIcon("/images/icons/create_doc.gif");
        Class[] params2 = {};
        MethodExpression me2 = ctx.getApplication()
                .getExpressionFactory()
                .createMethodExpression(ctx.getELContext(),
                        "#{dropDownMenuBean.doSearch}", String.class, params2);
        menItm2.setActionExpression(me2);
        menItm2.setMode(Mode.ajax);

        //Create an ID on your Item menu:
        menItm2.setId("doSearchId");

        dropDownMenu.getChildren().add(menItm2);

        menuBar.getChildren().add(dropDownMenu);

        return menuBar;
    }
}
//Ref https://community.jboss.org/message/607891